package io.github.talelin.latticy.controller.v1;


import com.baomidou.mybatisplus.core.metadata.IPage;
import io.github.talelin.autoconfigure.exception.NotFoundException;
import io.github.talelin.core.annotation.*;
import io.github.talelin.core.token.DoubleJWT;
import io.github.talelin.core.token.Tokens;
import io.github.talelin.latticy.common.mybatis.Page;
import io.github.talelin.latticy.common.util.PageUtil;
import io.github.talelin.latticy.model.WxUserDetailDO;
import io.github.talelin.latticy.service.WxUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import io.github.talelin.latticy.model.WxUserDO;
import io.github.talelin.latticy.vo.CreatedVO;
import io.github.talelin.latticy.vo.DeletedVO;
import io.github.talelin.latticy.vo.PageResponseVO;
import io.github.talelin.latticy.vo.UpdatedVO;

import javax.validation.constraints.Min;
import javax.validation.constraints.Max;
import javax.validation.constraints.Positive;

import org.springframework.web.bind.annotation.RestController;

/**
* @author generator@lms
* @since 2021-04-20
*/
@RestController
@RequestMapping("/v1/wx-user")
@PermissionModule("C端用户")
@Validated
public class WxUserController {
    @Autowired
    private DoubleJWT jwt;

    @RequestMapping("/getTokens")
    public Tokens getToken() {
        return jwt.generateTokens(1L);
    }

    @Autowired
    private WxUserService wxUserService;

    @PostMapping("")
    public CreatedVO create() {
        return new CreatedVO();
    }

    @PutMapping("/{id}")
    public UpdatedVO update(@PathVariable @Positive(message = "{id.positive}") Integer id) {
        return new UpdatedVO();
    }

    @DeleteMapping("/{id}")
    @PermissionMeta("删除用户")
    @GroupRequired
    @Logger(template = "{user.nickname}删除了一个C端用户")
    public DeletedVO delete(@PathVariable @Positive(message = "{id.positive}") Integer id) {
        WxUserDO wxUser = wxUserService.getById(id);
        if (wxUser == null) {
            throw new NotFoundException(20000);
        }
        wxUserService.getBaseMapper().deleteById(id);
        return new DeletedVO();
    }

    @GetMapping("/{id}")
    @LoginRequired
    @PermissionMeta("查看用户详情")
    @GroupRequired
    public WxUserDetailDO get(@PathVariable(value = "id") @Positive(message = "{id.positive}") Integer id) {
        return wxUserService.getDetail(id);
    }

    @GetMapping("/page")
    @LoginRequired
    public PageResponseVO<WxUserDO> page(
            @RequestParam(name = "count", required = false, defaultValue = "10")
            @Min(value = 1, message = "{page.count.min}")
            @Max(value = 30, message = "{page.count.max}") Integer count,
            @RequestParam(name = "page", required = false, defaultValue = "0")
            @Min(value = 0, message = "{page.number.min}") Integer page
    ) {
        Page<WxUserDO> pager = new Page<>(page,count);
        IPage<WxUserDO> paging = wxUserService.getBaseMapper().selectPage(pager, null);
        return PageUtil.build(paging);
    }

}
